﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Meirimoney.Exchange.Website.Utilities
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 当前的请求是否Get请求
        /// </summary>
        protected Boolean IsHttpGet
        {
            get
            {
                return this.Request.HttpMethod.Equals("GET");
            }
        }

        /// <summary>
        /// 当前的请求是否为Post请求
        /// </summary>
        protected Boolean IsHttpPost
        {
            get
            {
                return this.Request.HttpMethod.Equals("POST");
            }
        }

        /// <summary>
        /// PostValue:Post Value 获取Post访问方式，表单控件的值
        /// </summary>
        /// <param name="key">表单控件的名称</param>
        /// <returns>表单控件的值</returns>
        public String PostValue(String key)
        {
            return this.Request.Form[key];
        }

        /// <summary>
        /// QueryValue:Query Value:获取Get访问时的查询参数;
        /// </summary>
        /// <param name="queryString">查询变量的名称</param>
        /// <returns></returns>
        public String QueryValue(String queryString)
        {
            return this.Request.QueryString[queryString];
        }

        /// <summary>
        /// 弹出警告消息
        /// </summary>
        /// <param name="message">消息</param>
        public virtual void ShowMessage(string message)
        {
            WriteScript("alert('" + message + "')");
        }

        /// <summary>
        /// 提示后跳转
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <param name="location">跳转的url</param>
        public virtual void ShowMessage(String message, String location)
        {
            ShowMessage(message);
            WriteScript("window.location=" + location);
        }

        /// <summary>
        /// 向客户端添加脚本
        /// </summary>
        /// <param name="code">脚本内容</param>
        public void WriteScript(String code)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(),
            Guid.NewGuid().ToString(), ";" + code, true);
        }

        /// <summary>
        /// 错误处理
        /// </summary>
        /// <param name="message">错误信息</param>
        public virtual void ErrorHandle(String message)
        {
            Response.Write(message);
        }

        /// <summary>
        /// 错误处理并根据指示是否终止当前响应
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="endResponse">是否终止当前页面的响应</param>
        public void ErrorHandle(String message, Boolean endResponse)
        {
            ErrorHandle(message);
            if (endResponse)
                Response.End();
        }
    }
}